"""Simple echo server.

$ python3 echo.py
$ nc -C localhost 8013
"""

from diesel import Application, TCPServer, ConnectionClosed
from diesel import until_eol, send
from diesel import log

logger = log.name('echo')

def hi_server(addr):
    while True:
        try:
            inp = until_eol()
        except ConnectionClosed as exc:
            logger.error('[{etype}] {evalue}', etype=type(exc).__name__, evalue=str(exc))
            break
        if inp.strip() == b'quit':
            break
        send(b'you said ' + inp)

app = Application()
app.add_service(TCPServer(hi_server, 8013))
app.run()
